namespace SinaFinance.MainChartArea
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;

    public partial class SlideShow : UserControl
    {
        //private bool _contentLoaded;
        private int _currentPage = 0;
        private bool _odd = false;
        private ObservableCollection<UserControl> _slides = new ObservableCollection<UserControl>();
        //internal Grid evenPage;
        //internal Grid LayoutRoot;
        //internal Grid oddPage;
        //internal PlaneProjection planeProjection1;
        //internal PlaneProjection planeProjection2;
        //internal Storyboard reverseTransitionIn1;
        //internal Storyboard reverseTransitionIn2;
        //internal Storyboard reverseTransitionOut1;
        //internal Storyboard reverseTransitionOut2;
        //internal Storyboard transitionIn1;
        //internal Storyboard transitionIn2;
        //internal Storyboard transitionOut1;
        //internal Storyboard transitionOut2;

        public SlideShow()
        {
            this.InitializeComponent();
            base.Loaded += new RoutedEventHandler(this.SlideShow_Loaded);
        }

        public void GoNextPage()
        {
            if (this._currentPage > 0)
            {
                this.LoadPreviousPage();
                this.NavigatePrevious();
                this._currentPage--;
            }
        }

        public void GoPreviewPage()
        {
            if (this._currentPage < (this._slides.Count - 1))
            {
                this.LoadNextPage();
                this.NavigateNext();
                this._currentPage++;
            }
        }

        //[DebuggerNonUserCode]
        //public void InitializeComponent()
        //{
        //    if (!this._contentLoaded)
        //    {
        //        this._contentLoaded = true;
        //        Application.LoadComponent(this, new Uri("/SinaFinance;component/MainChartArea/SlideShow.xaml", UriKind.Relative));
        //        this.LayoutRoot = (Grid) base.FindName("LayoutRoot");
        //        this.oddPage = (Grid) base.FindName("oddPage");
        //        this.transitionIn1 = (Storyboard) base.FindName("transitionIn1");
        //        this.transitionOut1 = (Storyboard) base.FindName("transitionOut1");
        //        this.reverseTransitionIn1 = (Storyboard) base.FindName("reverseTransitionIn1");
        //        this.reverseTransitionOut1 = (Storyboard) base.FindName("reverseTransitionOut1");
        //        this.planeProjection1 = (PlaneProjection) base.FindName("planeProjection1");
        //        this.evenPage = (Grid) base.FindName("evenPage");
        //        this.transitionIn2 = (Storyboard) base.FindName("transitionIn2");
        //        this.transitionOut2 = (Storyboard) base.FindName("transitionOut2");
        //        this.reverseTransitionIn2 = (Storyboard) base.FindName("reverseTransitionIn2");
        //        this.reverseTransitionOut2 = (Storyboard) base.FindName("reverseTransitionOut2");
        //        this.planeProjection2 = (PlaneProjection) base.FindName("planeProjection2");
        //    }
        //}

        private void LoadNextPage()
        {
            if (this._currentPage < (this._slides.Count - 1))
            {
                if (this._odd)
                {
                    if (!this.oddPage.Children.Contains(this._slides[(int) (this._currentPage + 1)]))
                    {
                        this.oddPage.Children.Clear();
                        this.oddPage.Children.Add(this._slides[(int) (this._currentPage + 1)]);
                    }
                }
                else if (!this.evenPage.Children.Contains(this._slides[(int) (this._currentPage + 1)]))
                {
                    this.evenPage.Children.Clear();
                    this.evenPage.Children.Add(this._slides[(int) (this._currentPage + 1)]);
                }
            }
        }

        private void LoadPreviousPage()
        {
            if (this._currentPage > 0)
            {
                if (this._odd)
                {
                    if (!this.oddPage.Children.Contains(this._slides[(int) (this._currentPage - 1)]))
                    {
                        this.oddPage.Children.Clear();
                        this.oddPage.Children.Add(this._slides[(int) (this._currentPage - 1)]);
                    }
                }
                else if (!this.evenPage.Children.Contains(this._slides[(int) (this._currentPage - 1)]))
                {
                    this.evenPage.Children.Clear();
                    this.evenPage.Children.Add(this._slides[(int) (this._currentPage - 1)]);
                }
            }
        }

        private void NavigateNext()
        {
            if (this._odd)
            {
                ((Storyboard) base.FindName("transitionOut1")).Begin();
                ((Storyboard) base.FindName("transitionIn2")).Begin();
            }
            else
            {
                ((Storyboard) base.FindName("transitionOut2")).Begin();
                ((Storyboard) base.FindName("transitionIn1")).Begin();
            }
            this._odd = !this._odd;
        }

        private void NavigatePrevious()
        {
            if (!this._odd)
            {
                ((Storyboard) base.FindName("reverseTransitionOut1")).Begin();
                ((Storyboard) base.FindName("reverseTransitionIn2")).Begin();
            }
            else
            {
                ((Storyboard) base.FindName("reverseTransitionOut2")).Begin();
                ((Storyboard) base.FindName("reverseTransitionIn1")).Begin();
            }
            this._odd = !this._odd;
        }

        private void SlideShow_Loaded(object sender, RoutedEventArgs e)
        {
            if ((this.Slides != null) && !this.Slides.Count.Equals(0))
            {
                LinearGradientBrush brush = new LinearGradientBrush();
                GradientStop stop = new GradientStop();
                GradientStop stop2 = new GradientStop();
                GradientStop stop3 = new GradientStop();
                stop.Color = Color.FromArgb(0xff, 0, 0, 0);
                stop.Offset = 0.0;
                stop2.Color = Color.FromArgb(0xff, 0x13, 0x1b, 0x29);
                stop2.Offset = 0.5;
                stop3.Color = Color.FromArgb(0xff, 0x1c, 0x2e, 0x41);
                stop3.Offset = 1.0;
                brush.StartPoint = new Point(0.0, 0.0);
                brush.EndPoint = new Point(0.0, 1.0);
                brush.GradientStops.Add(stop);
                brush.GradientStops.Add(stop2);
                brush.GradientStops.Add(stop3);
                this.LayoutRoot.Background = brush;
                this.planeProjection1.CenterOfRotationZ = -this.Slides[0].Width / 2.0;
                this.planeProjection2.CenterOfRotationZ = -this.Slides[0].Width / 2.0;
            }
        }

        public void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            LinearGradientBrush brush = new LinearGradientBrush();
            GradientStop stop = new GradientStop();
            GradientStop stop2 = new GradientStop();
            GradientStop stop3 = new GradientStop();
            stop.Color = Color.FromArgb(0xff, 0, 0, 0);
            stop.Offset = 0.0;
            stop2.Color = Color.FromArgb(0xff, 0x13, 0x1b, 0x29);
            stop2.Offset = 0.5;
            stop3.Color = Color.FromArgb(0xff, 0x1c, 0x2e, 0x41);
            stop3.Offset = 1.0;
            brush.StartPoint = new Point(0.0, 0.0);
            brush.EndPoint = new Point(0.0, 1.0);
            brush.GradientStops.Add(stop);
            brush.GradientStops.Add(stop2);
            brush.GradientStops.Add(stop3);
            this.LayoutRoot.Background = brush;
            if (e.Key == Key.Right)
            {
                this.GoPreviewPage();
            }
            if (e.Key == Key.Left)
            {
                this.GoNextPage();
            }
        }

        public int CurrentPage
        {
            get
            {
                return this._currentPage;
            }
        }

        public int CurrentSlide
        {
            get
            {
                return this._currentPage;
            }
            set
            {
                this._currentPage = value;
                if (this._odd)
                {
                    this.evenPage.Children.Clear();
                    this.evenPage.Children.Add(this._slides[this._currentPage]);
                }
                else
                {
                    this.oddPage.Children.Clear();
                    this.oddPage.Children.Add(this._slides[this._currentPage]);
                }
            }
        }

        public ObservableCollection<UserControl> Slides
        {
            get
            {
                return this._slides;
            }
            set
            {
                this._slides = value;
            }
        }
    }
}
